package com.rwcw.admin.config.exception;

import com.rwcw.admin.vo.ResultVO;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * validator全局异常捕获
 */
@ControllerAdvice
public class ValidationExceptionHandler {
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultVO<String> validationExceptionHandler(MethodArgumentNotValidException exception) {
        String errorMsg = "";
        if (exception.getBindingResult().getFieldError() != null) {
            errorMsg = exception.getBindingResult().getFieldError().getDefaultMessage();
        }
        return new ResultVO<>(ExceptionCodeEnum.PRAM_NOT_MATCH.getCode(), errorMsg);
    }
}
